const useOrignalManagerList = (cfg) => {

    const { listManager, origListManager, billId, field = { pk: 'c_sort', effect: [], mapping: [] }, effectAction = [0, 1] } = cfg || {};




    // function mappingFild(dItem, sItem) {
    //     const map = new Map(field.mapping.map(map => {
    //         const [first, ...others] = map;
    //         return [first, [...others]];
    //     }));

    //     map.forEach((mapKeys, key) => {
    //         mapKeys.forEach((mapKey, index) => {
    //             (mapKey && (index === 0 || sItem[mapKey])) ? dItem[key] = (((typeof dItem[key] === 'number') && (sItem[mapKey] === undefined || sItem[mapKey] == null)) ? 0 : sItem[mapKey]) : false;
    //         });
    //     });

    //     const pks = Array.isArray(field.pk) ? field.pk : [field.pk];
    //     pks.forEach(k => dItem[k] = sItem[k]);
    // }

    if (!billId) {
        origListManager = [];
    }

    // function itemEqual(item1, item2) {
    //     const { pk } = field;
    //     const pks = Array.isArray(pk) ? pk : [pk];
    //     return pks.every(k => item1[k] === item2[k]);
    // }

    // function itemsChanged(e) {
    //     if (listManager) {
    //         if (e.action === 0 && effectAction.includes(0)) {
    //             const item = e.changedItems[0];
    //             const rowInOriData = origListManager.filter(a => itemEqual(a, item));
    //             if (!rowInOriData.length && !itemEqual(arrayRef, item)) {
    //                 const newRow = { ...item };
    //                 mappingFild(newRow, item);
    //                 origListManager.push([...newRow, rowType = 2]);
    //                 arrayRef = newRow;
    //             }
    //         } else if (e.action === 1 && effectAction.includes(1)) {
    //             arrayRef = {};
    //             const pkids = (e.changedItems).map(item => item);
    //             for (let i = 0; i < origListManager.length; i++) {
    //                 if (pkids.some(p => itemEqual(origListManager[i], p))) {
    //                     origListManager[i].rowType = 3;
    //                 }
    //             }
    //         }
    //     }
    // }

    // function fieldChanged(e) {
    //     if (field.effect.includes(e.fieldName)) {
    //         if (listManager.some(a => a === e.item)) {
    //             const row = origListManager.find(a => itemEqual(e.item, a));
    //             Object.keys(e.item).forEach(a => {
    //                 if (a !== '_key') {
    //                     row[a] = e.item[a];
    //                     mappingFild(row, e.item);
    //                 }
    //             });
    //         }
    //     }
    // }

    return { listManager, origListManager, billId, field, effectAction, itemsChanged, fieldChanged };
}

const mappingFild = (dItem, sItem, field) => {
    const map = new Map(field.mapping?.map(map => {
        const [first, ...others] = map;
        return [first, [...others]];
    }));

    map.forEach((mapKeys, key) => {
        mapKeys.forEach((mapKey, index) => {
            (mapKey && (index === 0 || sItem[mapKey])) ? dItem[key] = (((typeof dItem[key] === 'number') && (sItem[mapKey] === undefined || sItem[mapKey] == null)) ? 0 : sItem[mapKey]) : false;
        });
    });

    const pks = Array.isArray(field.pk) ? field.pk : [field.pk];
    pks.forEach(k => dItem[k] = sItem[k]);
}

const itemEqual = (item1, item2, field) => {
    const { pk } = field;
    const pks = Array.isArray(pk) ? pk : [pk];
    return pks.every(k => item1[k] === item2[k]);
}

const effectAction = [0, 1];
let arrayRef = {};
const itemsChanged = ({ origListManager, listManager, action, billId, changedItems, field = { pk: 'c_sort', effect: [], mapping: [] } }) => {
    // if (!billId) {
    //     origListManager = [];
    // }
    if (listManager) {
        if (action === 0 && effectAction.includes(0)) {
            const item = changedItems[0];
            const rowInOriData = origListManager.filter(a => itemEqual(a, item, field));
            if (!rowInOriData.length && !itemEqual(arrayRef, item, field)) {
                const newRow = { ...item };
                mappingFild(newRow, item, field);
                origListManager.push({ ...newRow, rowType: 1 });
                arrayRef = newRow;
            }
        } else if (action === 1 && effectAction.includes(1)) {
            arrayRef = {};
            const pkids = (changedItems).map(item => item);
            for (let i = 0; i < origListManager.length; i++) {
                if (pkids.some(p => itemEqual(origListManager[i], p,field))) {
                    // origListManager[i].rowType = 3;
                    origListManager.splice(i, 1);
                }
            }
        }
        return origListManager;
    }
}

const fieldChanged = (e) => {
    if (field.effect.includes(e.fieldName)) {
        if (listManager.some(a => a === e.item)) {
            const row = origListManager.find(a => itemEqual(e.item, a));
            Object.keys(e.item).forEach(a => {
                if (a !== '_key') {
                    row[a] = e.item[a];
                    mappingFild(row, e.item);
                }
            });
        }
    }
}

export { itemsChanged };